package com.example.demo.Controller;

import com.example.demo.Comm.Resultajax;
import com.example.demo.Comm.SessionUtils;
import com.example.demo.Model.ArtInfo;
import com.example.demo.Model.Userinfo;
import com.example.demo.Model.VO.UserinfoVO;
import com.example.demo.Service.ArticleService;
import com.example.demo.Service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@RestController
@RequestMapping("/art")
public class ArticleController {

    private static final int _DESC_LENGTH = 120;
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private Userservice userservice;
    @RequestMapping("/mylist")
    public Resultajax MyList(HttpServletRequest request) {
        Userinfo userinfo = SessionUtils.GetUser(request);
        if(userinfo == null) {
            return Resultajax.fail(-1,"请先登录");
        }
        List<ArtInfo> list = articleService.getArtById(userinfo.getId());
        if(list != null && list.size() > _DESC_LENGTH) {
            list.stream().parallel().forEach((art) -> {
                if (art.getContent().length() > _DESC_LENGTH) {
                    art.setContent(art.getContent().substring(0, _DESC_LENGTH));
                }
            });
        }
        return Resultajax.succ(list);
    }

    @RequestMapping("/del")
    public Resultajax delArt(Integer aid,HttpServletRequest request) {
        if(aid == null || aid<=0) {
            return Resultajax.fail(-1,"参数错误");
        }
        Userinfo userinfo = SessionUtils.GetUser(request);
        if(userinfo == null) {
            return Resultajax.fail(-1,"参数错误");
        }

        int res = articleService.del(aid, userinfo.getId());

        return Resultajax.succ(res);
    }


    @RequestMapping("/add")
    public Resultajax add(ArtInfo artInfo,HttpServletRequest request) {
        if(artInfo == null || !StringUtils.hasLength(artInfo.getTitle()) ||
                !StringUtils.hasLength(artInfo.getContent())) {
            return Resultajax.fail(-1,"参数错误");
        }

        Userinfo userinfo = SessionUtils.GetUser(request);
        if(userinfo == null) {
            return Resultajax.fail(-2,"请先登录");
        }
        artInfo.setUid(userinfo.getId());
        int result = articleService.add(artInfo);
        return Resultajax.succ(result);
    }

    @RequestMapping("update_init")
    public Resultajax updateInit(Integer aid,HttpServletRequest request) {
        if(aid == null || aid <= 0) {
            return Resultajax.fail(-1,"参数有误");
        }
        Userinfo userinfo = SessionUtils.GetUser(request);
        if(userinfo == null) {
            return Resultajax.fail(-2,"请先登录");
        }

        ArtInfo artInfo = articleService.getArticleByAid(aid,userinfo.getId());
        return Resultajax.succ(artInfo);
    }

    @RequestMapping("update")
    public Resultajax doUpdate(ArtInfo artInfo, HttpServletRequest request) {
        if(artInfo == null || !StringUtils.hasLength(artInfo.getTitle()) ||
                !StringUtils.hasLength(artInfo.getContent()) || artInfo.getId() == 0)  {
            return Resultajax.fail(-1,"参数错误");
        }
        Userinfo userinfo = SessionUtils.GetUser(request);
        if(userinfo == null) {
            return Resultajax.fail(-2,"请先登录");
        }

        artInfo.setUid(userinfo.getId());
        int result = articleService.doupdate(artInfo);

        return Resultajax.succ(result);
    }

    @RequestMapping("/detail")
    public Resultajax detail(Integer aid) throws ExecutionException, InterruptedException {
        if(aid == null || aid <= 0) {
            return Resultajax.fail(-1,"参数错误");
        }
        ArtInfo artInfo = articleService.getArticleById(aid);
        if(artInfo == null) {
            return Resultajax.fail(-1,"参数错误");
        }

        FutureTask<UserinfoVO> userTask = new FutureTask<>(() -> {
            return userservice.getUserById(artInfo.getUid());
        });
        taskExecutor.submit(userTask);

        FutureTask<Integer> artCountTask = new FutureTask<>(() -> {
            return articleService.GetUserArtCount(artInfo.getUid());
        });
        taskExecutor.submit(artCountTask);
        UserinfoVO userinfoVO = userTask.get();
        int artCount = artCountTask.get();
        userinfoVO.setArtCount(artCount);
        HashMap<String,Object> hash = new HashMap<>();
        hash.put("user",userinfoVO);
        hash.put("art",artInfo);
        return Resultajax.succ(hash);
    }

    @RequestMapping("/increment_Rcount")
    public Resultajax incrementRCount(Integer aid) {
        if(aid == null || aid <= 0) {
            return Resultajax.fail(-1,"参数有误");
        }

        int ret = articleService.incrementRCount(aid);

        return Resultajax.succ(ret);
    }

    @RequestMapping("/getlistbypag")
    public Resultajax getListByPag(Integer psize,Integer pindex) throws ExecutionException, InterruptedException {
         if(psize == null || psize <= 0) {
            psize = 2;
        }
        if(pindex == null || pindex <= 0) {
            pindex = 1;
        }

        int offest = psize*(pindex-1);
        int finalpsize = psize;
        FutureTask<List<ArtInfo>> ListTask = new FutureTask<>(() -> {
           return articleService.getListByPage(finalpsize,offest);
        });

        FutureTask<Integer> sizeTask = new FutureTask<>(() -> {
            int count = articleService.getCount();
            double size = (count*1.0)/(finalpsize*1.0);
           return (int)Math.ceil(size);
        });

        taskExecutor.submit(ListTask);
        taskExecutor.submit(sizeTask);

        List<ArtInfo> list = ListTask.get();
        int size = sizeTask.get();
        Map<String,Object> hash = new HashMap<>();
        hash.put("list",list);
        hash.put("size",size);
        return Resultajax.succ(hash);
    }

}
